package com.example.text;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;

import com.example.text.Adapter.fr_Adapter;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottom;
    private ViewPager viewPager;

    private List<Fragment> fragments;
    private Fragment Fragment_index,Fragment_lxr,Fragment_w;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
        initfunction();
    }

    private void initview() {
        bottom = findViewById(R.id.navigation);
        viewPager=findViewById(R.id.view_pager);
        fragments=new ArrayList<>();
    }

    private void initfunction() {
        Fragment_index=new Fragment_index();
        Fragment_lxr=new Fragment_lxr();
        Fragment_w=new Fragment_w();

        fragments.add(Fragment_index);
        fragments.add(Fragment_lxr);
        fragments.add(Fragment_w);

            fr_Adapter fr_adapter = new fr_Adapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(fr_adapter);
        viewPager.setCurrentItem(0);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

            @Override
            public void onPageSelected(int position) {
                bottom.setSelectedItemId(bottom.getMenu().getItem(position).getItemId());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_search:
                        viewPager.setCurrentItem(0);
                        return true;
                    case R.id.action_settings:
                        viewPager.setCurrentItem(1);
                        return true;
                    case R.id.action_navigation:
                        viewPager.setCurrentItem(2);
                        return true;
                }
                return false;
            }
        });
    }
}


